package project;

import java.util.Scanner;

public class project_4 {
    public static void main(String[] args) {
        User[] users = new User[50]; // 创建users数组 存取50个对象
        System.out.println();
        while (true) {
            System.out.println("通讯录");
            System.out.println("===========================");
            System.out.println("1.新增");
            System.out.println("2.修改");
            System.out.println("3.查看");
            System.out.println("4.删除");
            System.out.println("5.退出");
            System.out.println("===========================");
            System.out.print("请输入功能编号：");
            Scanner sc = new Scanner(System.in);
            String str = sc.next();
            if (str.equals("1")) {
                User user = new User(); // 创建user对象
                System.out.print("姓名:");
                user.name = sc.next();
                System.out.print("电话号码:");
                user.phone = sc.next();
                for (int i = 0; i < users.length; i++) {
                    if (users[i] == null) {
                        users[i] = user;
                        break;
                    }
                }
                System.out.println("新增成功");

            } else if (str.equals("2")) {
                System.out.print("姓名:");
                String name = sc.next();
                System.out.print("请输入新的电话号码:");
                String phone = sc.next();
                for (int i = 0; i < users.length; i++) {
                    if (users[i] != null && users[i].name.equals(name)) {
                        users[i].phone = phone;
                    }
                }
                System.out.println("修改成功");
            } else if (str.equals("3")) {
                System.out.println("姓名\t电话");
                System.out.println("=================");
                for (User user : users) {
                    if (user != null)
                        System.out.println(user.name + "\t" + user.phone);
                }
            } else if (str.equals("4")) {
                System.out.print("姓名:");
                String name = sc.next();
                for (int i = 0; i < users.length; i++) {
                    if (users[i] != null && users[i].name.equals(name)) {
                        users[i] = null;
                    }
                }
                System.out.println("删除成功");
            } else if (str.equals("5")) {
                System.out.println("退出成功");
                break;
            } else {
                System.out.println("请重新输入");
                break;
            }
        }
    }
}
